﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using com.nm.ApiServer.Attributes;
using com.nm.library.Excels;
using System.Text.RegularExpressions;

namespace VerticaAPI.Export
{
    [Export(typeof(ExcelImport))]
    public class DWSuppleAllMessage : ExcelImport
    {
        public override ExcelImportType Type => ExcelImportType.REPLENISHMENT;
        public override Dictionary<string, ImportVerify> DictFields => _dictFields;
        /// <summary>
        /// Excel字段映射
        /// </summary>
        private static readonly Dictionary<string, ImportVerify> _dictFields = new List<ImportVerify> {
            new ImportVerify{ ColumnName="配货仓代码",FieldName="FHStockCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="配货仓名称",FieldName="FHStockName",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,200,true,true)},
            new ImportVerify{ ColumnName="收货仓代码",FieldName="StockCode",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="收货仓名称",FieldName="StockName" ,DataType=typeof(string),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,250,false,false)},
            new ImportVerify{ ColumnName="货品ID",FieldName="MaterialID",DataType=typeof(string),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,100,true,true)},
            new ImportVerify{ ColumnName="S",FieldName="S",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="M",FieldName="M",DataType=typeof(int),VerifyFunc =(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="L",FieldName="L",DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true) },
            new ImportVerify{ ColumnName="XL",FieldName="XL",DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true) },
            new ImportVerify{ ColumnName="XXL",FieldName="XXL" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="3XL",FieldName="3XL" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="4XL",FieldName="4XL" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="JM",FieldName="JM" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="27",FieldName="27" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="28",FieldName="28" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="29",FieldName="29" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="30",FieldName="30" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="31",FieldName="31" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="32",FieldName="32" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="33",FieldName="33" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="34",FieldName="34" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="35",FieldName="35" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="36",FieldName="36" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="37",FieldName="37" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="38",FieldName="38" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="39",FieldName="39" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="40",FieldName="40" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="41",FieldName="41" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="42",FieldName="42" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="43",FieldName="43" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="44",FieldName="44" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)},
            new ImportVerify{ ColumnName="45",FieldName="45" ,DataType=typeof(int),VerifyFunc=(e,extra)=> ExcelImportHelper.GetCellMsg(e.CellValue,e.ColName,2,false,true)}
           }.ToDictionary(e => e.ColumnName, e => e);

        public override object SaveImportData(DataTable dt, Dictionary<string, object> extraInfo, int userId)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 获取导出的数据
        /// </summary>
        /// <param name="Keys">查询筛选条件</param>
        /// <returns>Datatable</returns>
        public override DataTable GetExportData(string Keys)
        {
            var db = new DWSuppleAllMessageHandler();
            //var strWhere = string.Empty;
            //if (!string.IsNullOrWhiteSpace(Keys))
            //{
            //    var field = Keys.Split(',');
            //    foreach (var v in field)
            //    {
            //        var keyValuePair = v.Split(':');
            //        var key = keyValuePair[0]?.Trim();
            //        strWhere += key + " like '%" + keyValuePair[1] + "%'";
            //        //判断如果有N个参数，要在参数之后增加N个AND
            //        strWhere += " AND ";
            //    }
            //    //删除最后一个AND
            //    strWhere = strWhere.Substring(0, strWhere.Length - 4);
            //}
            var dt = db.ExportDWSuppleAllMessageList(Keys);
            var regNum = new Regex("^[N]");
            for (var i = 0; i < dt.Columns.Count; i++)
            {
                if (regNum.IsMatch(dt.Columns[i].ColumnName))
                {
                    dt.Columns[i].ColumnName = dt.Columns[i].ColumnName.Substring(1, dt.Columns[i].ColumnName.Length - 1);
                }
            }
            return dt;
        }
        
    }
}
